//
//  BGAuctipnAddTableViewCell.m
//  bigMall
//
//  Created by LTOVE on 16/10/11.
//  Copyright © 2016年 thinklev. All rights reserved.
//
#import "BGAuctionAddCellModel.h"
#import "BGAuctipnAddTableViewCell.h"
static NSString *const reuseIdentify = @"cell";

@interface BGAuctipnAddTableViewCell ()<UITextFieldDelegate>

@property (nonatomic,weak)UITextField *textFild;
@property (nonatomic,weak)UILabel *label;
@end

@implementation BGAuctipnAddTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(BGAuctionAddCellModel *)model
{
    _model = model;
    [self setUpAllSubviewsWith:model];
    
}

- (void)setUpAllSubviewsWith:(BGAuctionAddCellModel *)model
{
    if ([model isKindOfClass:[BGAuctionAddCellModelTextFild class]]) {
        //填写的
        BGAuctionAddCellModelTextFild *itemModel = (BGAuctionAddCellModelTextFild *)model;
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        UIView *contentView = [[UIView alloc]initWithFrame:self.bounds];
        [self addSubview:contentView];
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = itemModel.title;
        titleLabel.font = Font30;
        titleLabel.textColor = textBlackColor;
        [titleLabel sizeToFit];
        titleLabel.x = BGMaginLeft;
        titleLabel.centerY = contentView.height * 0.5;
        [contentView addSubview:titleLabel];
        NSDictionary *dic = @{
                              NSFontAttributeName:Font30
                              };
        CGSize size = [itemModel.textFildValue sizeWithAttributes:dic];
        UITextField *textFile = [UITextField new];
        textFile.text = itemModel.textFildValue;
        textFile.size = size;
        textFile.x = CGRectGetMaxX(titleLabel.frame) + 5;
        textFile.centerY = titleLabel.centerY;
        textFile.font = Font30;
        textFile.textColor = read_;
        textFile.keyboardType = UIKeyboardTypeDecimalPad;
        textFile.delegate = self;
        [textFile addTarget:self action:@selector(textFildChange:) forControlEvents:UIControlEventAllEvents];
        self.textFild = textFile;
        [contentView addSubview:textFile];
        UILabel *titleLabels = [UILabel new];
        titleLabels.text = @"元";
        titleLabels.font = Font30;
        titleLabels.textColor = read_;
        self.label = titleLabels;
        [titleLabels sizeToFit];
        titleLabels.x = CGRectGetMaxX(textFile.frame);
        titleLabels.centerY = contentView.height * 0.5;
        [contentView addSubview:titleLabels];
        
    }else{
        NSDictionary *dic = @{
                              NSFontAttributeName:Font30,
                              NSForegroundColorAttributeName:textBlackColor
                              };
        NSRange range = [model.title rangeOfString:@":"];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:model.title attributes:dic];
        [attrStr addAttribute:NSForegroundColorAttributeName value:read_ range:NSMakeRange(range.location, model.title.length - range.location -1)];
        
        self.textLabel.attributedText = attrStr;
        
        
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(textFildChanged:)]) {
        [_delegate textFildChanged:textField.text];
    }
}

- (void)textFildChange:(UITextField *)textField
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font30
                          };
    
    CGSize size = [textField.text sizeWithAttributes:dic];
    self.textFild.size = size;
    self.label.x = CGRectGetMaxX(self.textFild.frame);
    
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGAuctipnAddTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
    }
    return cell;
}


@end
